﻿using Assets.Scripts.DL.ComponentScripts;
using Assets.Scripts.DL.Consts;
using Assets.Scripts.DL.Enemy.CommonEnemy;
using Assets.Scripts.DL.Mgrs;
using System;
using System.Collections;
using System.Xml.Schema;
using UnityEngine;
using UnityEngine.UIElements;

namespace Assets.Scripts.DL.Enemy.YaSuo {
	public class YasuoSLGT : CommonState<YasuoController> {
		//0.7 0.8
		bool dmgsend;
		int curSLGTIndex;
		//bool animStart;
		Timer atkWaitTimer = new();
		bool secondShadow;
		bool last;
		bool lastSendDmg;
		bool ShadowOver;
		public YasuoSLGT(YasuoController controller, StateEnum stateName) : base(controller, stateName) {

			AddConditionToSelf(() => _controller.IsPlayerKnockFly());
		}

		public override void Enter() {
			_controller.transform.position = _controller.Player.transform.position;
			_controller.Velocity = Vector2.zero;
			_controller.SetGravity(0);
			//_controller.PlayAnim(StateEnum.SLGT);//有问题
			dmgsend = true;
			curSLGTIndex = 0;

			last = false;
			//animStart = false;
			SetAlpha(0);
			atkWaitTimer.Reset();
			//播放劫
			secondShadow = false;
			lastSendDmg = false;
			ShadowOver = false;
			AudioManager.Play(_controller.Voice2, AudioStr.SLGT, false);
			_controller.StartCoroutine(FourShadow());
		}
		public override void Update() {
			//if (atkWaitTimer.Elapsed > _controller.EachSLGTWaitTime && !secondShadow) {
			//	atkWaitTimer.Reset();
			//	secondShadow = true;
			//	MakeShadow(1, _controller.ShadowSpeed, 30, _controller.ShadowDisFromPlayer);
			//}
			//else 
			if (ShadowOver && !last && atkWaitTimer.Elapsed > _controller.EachSLGTWaitTime) {
				_controller.PlayAnim(StateEnum.SLGT);
				SetAlpha(1);
				_controller.transform.position = _controller.GetPlayerPos();
				last = true;
				//secondShadow = false;
				//播放音效slgt
			}
			else if (last) {
				if (!lastSendDmg)
					SetPlayerVlocity(Vector2.zero);
				if (_controller.IsAnimPlayAt(0.75f) && !lastSendDmg) {
					//Debug.Log("send");
					lastSendDmg = true;
					_controller.Velocity = new Vector2(0, _controller.SLGTDownSpeed);
					_controller.Player.GetComponent<PlayerController>().Velocity = _controller.Velocity;
					ScreenEffectMgr.Instance.CutEffect(_controller.transform.position, 90);
					CameraImpulseMgr.Instance.Generate(Vector3.down);
				}
				else if (_controller.IsAnimPlayOver() && _controller.IsOnGround()) {
					_controller.StateData.AtkOver = true;
					_controller.Player.GetComponent<PlayerController>().StateData.IsKnockFly = false;
				}
			}
		}

		IEnumerator FourShadow() {
			var dir1 = new Vector3(1, -1, 1);
			var dir2 = new Vector3(1, 1, 1);
			MakeShadow(1, _controller.ShadowSpeed, -30, _controller.ShadowDisFromPlayer);
			CameraImpulseMgr.Instance.Generate(dir1);
			yield return new WaitForSeconds(0.5f);
			MakeShadow(1, _controller.ShadowSpeed, 30, _controller.ShadowDisFromPlayer);
			CameraImpulseMgr.Instance.Generate(dir2);
			yield return new WaitForSeconds(0.5f);
			MakeShadow(-1, _controller.ShadowSpeed, 160, _controller.ShadowDisFromPlayer);
			CameraImpulseMgr.Instance.Generate(dir1);
			yield return new WaitForSeconds(0.5f);
			SetPlayerVlocity(Vector2.zero);
			MakeShadow(-1, _controller.ShadowSpeed, -160, _controller.ShadowDisFromPlayer);
			CameraImpulseMgr.Instance.Generate(dir2);
			yield return new WaitForSeconds(0.5f);
			ShadowOver = true;
		}
		public override void Exit() {
			_controller.Velocity = Vector2.zero;
			_controller.ResetGravity();
		}

		void MakeShadow(int dir, float shadowSpeed, float angle, float disFromPlayer) {
			var radAngle = Mathf.Deg2Rad * angle;
			var vDir = new Vector2(Mathf.Cos(radAngle), Mathf.Sin(radAngle)).normalized;
			var pos = _controller.GetPlayerPos() - (Vector3)vDir * disFromPlayer;
			var obj = ObjectPool.Instance.GetObject(ObjPoolStr.YsShadow);
			var script = obj.GetComponent<YsShadow>();
			//Debug.Log($"SLGTshadow VDir{vDir}");
			script.Init(dir, vDir * shadowSpeed, angle, pos);
		}

		void SetPlayerVlocity(Vector2 velocity) {
			_controller.Player.GetComponent<PlayerController>().Velocity = velocity;
		}

		void SetAlpha(float alpha) {
			var c = _controller.SpriteRenderer.color;
			c.a = alpha;
			_controller.SpriteRenderer.color = c;
		}
	}
}
